package com.sphd.emanu.utils;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.sphd.emanu.config.Constant;
import com.wzutils.L;
import com.wzutils.TimeUtils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;

/**
 * @Author Hu
 * @Time 2017/7/10 --15:44.
 * @Email 87683202@qq.com
 */
public class GetTimeTask extends AsyncTask<String, Void, String> {
    Handler handler;
    String webUrl;
    public GetTimeTask(Handler handler,String webUrl) {
        this.handler=handler;
        this.webUrl=webUrl;
    }

    @Override
    protected String doInBackground(String... params) {
        L.e("doInBackground==="+params.toString());
        return getWebsiteDatetime(webUrl);
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        L.e("onPostExecute==="+s+ TimeUtils.longToString(Long.parseLong(s),TimeUtils.YEAR_MONTH_DAY_HOUR_MIN_SS));
        Message message=new Message();
        Bundle bundle=new Bundle();
        bundle.putString(Constant.NET_TIME,s);
        message.setData(bundle);
        handler.sendMessage(message);

    }

    private String getWebsiteDatetime(String webUrl) {
        try {
            URL url = new URL(webUrl);// 取得资源对象
            URLConnection uc = url.openConnection();// 生成连接对象
            uc.connect();// 发出连接
            long ld = uc.getDate();
            L.e("getWebsiteDatetime==="+String.valueOf(ld));
            // 读取网站日期时间
            return String.valueOf(ld);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
